package com.team2.outpatient_sys.web;

import com.team2.outpatient_sys.pojo.Patient;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.pojo.User;
import com.team2.outpatient_sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findById")
    public Result findById(int id) {
        Result result = new Result();
        User user = userService.findById(id);
        if (user == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(user);
        }
        return result;
    }

    @PutMapping("/add")
    public Result add(@RequestBody User user) {
        Result result = new Result();
        if (userService.add(user)) {
            result.setCode(200);
            result.setMsg("添加成功");
        } else {
            result.setCode(500);
            result.setMsg("添加失败");
        }
        return result;
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        Result result = new Result();
        if (userService.update(user)) {
            result.setCode(200);
            result.setMsg("修改成功");
        } else {
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }

    @PostMapping("/bind/{userId}")
    public Result bind(@RequestBody Patient patient,@PathVariable("userId") int userId) {
        Result result = new Result();
        if (userService.bind(patient, userId)) {
            result.setCode(200);
            result.setMsg("绑定成功");
        } else {
            result.setCode(500);
            result.setMsg("绑定失败");
        }
        return result;
    }
}
